package cc.laop.business.system;

import cc.laop.dao.system.LogMapper;
import cc.laop.entity.Page;
import cc.laop.entity.system.Log;
import cc.laop.entity.system.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class LogBusiness {

	@Autowired
	private LogMapper logMapper;

	public PageInfo<Log> selectByPage(Log log, Page page) {
		if (page == null || page.getPageSize() <= 0) {
			page = new Page(1, 10);
		}
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Log> list = logMapper.select(log);
		PageInfo<Log> pageinfo = new PageInfo<Log>(list);
		return pageinfo;
	}

	public int log(String desc, User user) {
		Log log = new Log();
		log.setUsername(user.getUsername());
		log.setOprTime(new Date());
		log.setDescription(desc);
		return logMapper.insert(log);
	}

	public int log(Log log, User user) {
		if (log != null) {
			log.setOprTime(new Date());
			return logMapper.insert(log);
		}
		return 0;
	}

}
